import {
  getScheduleDetailFun,
  addItemReq,
  updateItemReq,
  deleteItemReq,
  toggleStateReq,
  queryItemInfoReq,
  getAllVenueListFun,
  getAllClassRoomFun,
  addScheduleFun,
  getCourseListFun,
  getCoachListFun,
  getScheduleListFun,
  timeWeekFun,
  signInFun,
  clearScheduleFun,
  copyScheduleFun,
} from "./services";
import { namespace } from "./configFile";
import { message } from "antd";

export default {
  namespace,
  state: {
    init: false,
    venueList: [],
    classRoomList: [],
    courseList: [],
    coachList: [],
    scheduleList: [], //排课表
    weekData: [], //选中的星期的数据
  },
  effects: {
    //获取所有场馆
    *getVenueList({ params, callback }, { call, put }) {
      const response = yield call(getAllVenueListFun, { params });
      if (response.code === 200) {
        yield put({
          type: "save",
          payload: {
            venueList: response?.data,
          },
        });
        callback && callback(response.data);
      }
    },
    //排课表
    *getScheduleList({ params, callback }, { call, put }) {
      const response = yield call(getScheduleListFun, params);
      yield put({
        type: "save",
        payload: {
          scheduleList: response,
        },
      });
      callback && callback(response);
    },
    //选中的星期的数据
    *timeWeek({ params, callback }, { call, put }) {
      const response = yield call(timeWeekFun, params);
      if (response.code === 200) {
        yield put({
          type: "save",
          payload: {
            weekData: response?.data,
          },
        });
        callback && callback(response?.data);
      }
    },
    //获取所有教室
    *getAllClassRoom({ params, callback }, { call, put }) {
      const response = yield call(getAllClassRoomFun, params);
      if (response.code === 200) {
        yield put({
          type: "save",
          payload: {
            classRoomList: response?.data,
          },
        });
        callback && callback(response.data);
      }
    },
    //指定场馆下的所有课程
    *getCourseList({ params }, { call, put }) {
      const response = yield call(getCourseListFun, params);
      if (response.code === 200) {
        yield put({
          type: "save",
          payload: {
            courseList: response?.data,
          },
        });
      }
    },
    //所有教练
    *getCoachList({ params, callback }, { call, put }) {
      const response = yield call(getCoachListFun, params);
      yield put({
        type: "save",
        payload: {
          coachList: response,
        },
      });
      callback && callback(response);
    },
    //新增排课
    *addSchedule({ params, callback }, { call, put }) {
      const response = yield call(addScheduleFun, params);
      if (response.code === 200) {
        message.success(response.msg);
        callback && callback(response);
      }
    },
    //校验上课码
    *getScheduleDetail({ params, callback }, { call, put }) {
      const response = yield call(getScheduleDetailFun, params);
      if (response.code === 200) {
        callback && callback(response.data);
      } else {
        message.warning(response.msg);
      }
    },
    //签到
    *signIn({ params, callback }, { call, put }) {
      const response = yield call(signInFun, params);
      if (response.code === 200) {
        callback && callback(response);
      } else {
        message.warning(response.msg);
      }
    },
    //清空排课表
    *clearSchedule({ params, callback }, { call, put }) {
      const response = yield call(clearScheduleFun, params);
      if (response.code === 200) {
        if (response.data) callback && callback(response);
        else message.success(response.msg);
      }
    },
    //复制课程到指定周
    *copySchedule({ params, callback }, { call, put }) {
      const response = yield call(copyScheduleFun, params);
      if (response.code === 200) {
        callback && callback(response);
      }
    },
    *submitItem({ params, callback }, { call }) {
      let res = null;
      if (params.id) {
        res = yield call(updateItemReq, params);
      } else {
        res = yield call(addItemReq, params);
      }
      if (res?.code === 200) {
        message.success(res.msg);
        callback && callback();
      } else {
        message.error(res.msg);
      }
    },

    *deleteItem({ params, callback }, { call }) {
      let res = yield call(deleteItemReq, params);
      if (res?.code === 200) {
        message.success(res.msg);
        callback && callback();
      } else {
        message.error(res.msg);
      }
    },
    *queryItemInfo({ params, callback }, { call }) {
      let res = yield call(queryItemInfoReq, params);
      if (res) {
        callback && callback(res);
      }
    },
    *toggleState({ params, callback }, { call }) {
      let res = yield call(toggleStateReq, params);
      if (res) {
        callback && callback();
      }
    },
  },
  reducers: {
    save(state: any, { payload }: any) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
